//###############
// FILE : TextFileSearch.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION: Searching a string in a file.
//###############
package oop.ex3.fileSearchModel.SearchEngine;
import java.util.Iterator;
import java.io.*;
import java.util.ArrayList;

import oop.ex3.fileSearchModel.filters.*;
import oop.ex3.fileSearchModel.filters.Conditionable.STATUS;
import oop.ex3.fileSearchModel.util.exceptions.ReaderIOException;
/**
 * Searching a string in a file.
 */
public class TextFileSearch extends FileSearcher {
    //***********************************************************************
	//  Data members :                                                      *
	//***********************************************************************
	private ArrayList<Filter> _keywords;
    private BufferedReader _reader;
    //********************************************************************
	//  Finals :                                                         *
	//********************************************************************
	private final String IO_EXCEPTION_MESSAGE = "Error:I/O failure";
    //***********************************************************************
	//  Methods :                                                           *
	//***********************************************************************
    /**
     * Construct a new text file searcher for the file.
     * @param keywords The list of the filters with text to be searched
     * in the file.
     * @throws ReaderIOException in case of I/O error.
     */
    public TextFileSearch( ArrayList<Filter> keywords )
    throws ReaderIOException{
        super(keywords.get(0).getFile());
        _keywords = keywords;
        try{
            _reader = new BufferedReader( new FileReader(_file ) );
        }
        catch( FileNotFoundException e ) {

            throw new ReaderIOException(IO_EXCEPTION_MESSAGE);
        }   
    }
    /**
     * Sets the status of text filters.
     * @return True if there are text filters left which their
     * content was not found, false otherwise.
     */
    public boolean startSearch(){
        String str;
        try{
            while ( ( str = _reader.readLine() ) != null 
                    && !_keywords.isEmpty()) {
                ArrayList<Filter> list = searchInData(str);
                if ( !list.isEmpty() ) {
                    setNodesStatus(list, STATUS.TRUE);
                    _keywords.removeAll(list);
                    return true;
                }
            }
            close();
        }
        catch( IOException e ) {
            System.err.println(IO_EXCEPTION_MESSAGE);
        }
        setNodesStatus(_keywords, STATUS.FALSE);
        return false;
    }
    /**
     * Search file's data for desired content.
     * @param str array to look in.
     * @return A list with all files that contains the searched content.
     */
    private ArrayList<Filter> searchInData( String str ) {
        Iterator<Filter> it = _keywords.iterator();
        ArrayList<Filter> list = new ArrayList<Filter>();
        while ( it.hasNext() ) {
            Filter node = it.next();
            if ( str.contains(node.getData()) ) {
                list.add(node);
            }
        }
        return list;
    }
    /**
     * Close the stream.
     * @throws IOException In case of error in stream close.
     */    
    public void close() throws IOException {
        try{
            _reader.close();
        }
        catch ( IOException e ) {
            throw e;
        }
    }
}
